package com.ricky.utils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class Utils {

	private static Logger log = Logger.getLogger(Utils.class);

	public static ObjectMapper mapper = new ObjectMapper();

	@SuppressWarnings("unchecked")
	public static Map<String,Object> jsonToMap(String json)
	{
		try {
			Map<String,Object> re=mapper.readValue(json, HashMap.class);
			return re;
		} catch (JsonParseException e) {
			log.error(e.getMessage(),e);
		} catch (JsonMappingException e) {
			log.error(e.getMessage(),e);
		} catch (IOException e) {
			log.error(e.getMessage(),e);
		}
		return null;
	}
	
	public static String mapToJson(Map<String,?> m)
	{
		String re="";
		try {
			re = mapper.writeValueAsString(m);
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return re;
	}
}
